using System;
using Communication.Packets;
using System.IO;
using Utilities.Conversion;
using Utilities.IO;

namespace Communication.Protocol.General
{
    /// <summary>
    /// Acknowledge packet
    /// </summary>
    public sealed class AckPacket : Packet
    {
		public new static int ID = 0;

        #region Ctor
        /// <summary>
        /// Ctor
        /// </summary>
        public AckPacket()
            : base(PacketType.C1, ID)
        {
			this.m_isOK = true;
        }
        #endregion

        #region Serialization & Deserialization

        public AckPacket(BufferedStream buffer)
            : base(buffer)
        {
            // check if this is the right packet type.
            if (this.m_id != ID)
            {
                throw new InvalidPacketIDError();
            }
            EndianBinaryReader reader = new EndianBinaryReader(EndianBitConverter.Big, buffer);
            this.m_isOK = reader.ReadBoolean();
        }
		
		public override void Serialize(BufferedStream buffer)
        {
			EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, buffer);
            writer.Write(this.m_isOK);
           
            base.Serialize(buffer);

            // TODO: Change this behaviour to something more elegant.
            buffer.Seek(0, SeekOrigin.Begin);
        }

        #endregion

        #region Fields
        private bool m_isOK;
        #endregion
    }
}
